package cn.cs.service;

import cn.cs.common.MD5;
import cn.cs.common.RedisUtils;
import cn.cs.common.UserAgentUtil;
import cn.cs.mapper.AgentUserMapper;
import com.alibaba.fastjson.JSON;
import cz.mallat.uasparser.UserAgentInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class AgentUserService {
    private String tokenPrefix = "token:";
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private AgentUserMapper agentUserMapper;
    @RequestMapping(value = "/getAgentUserByPhone", method = RequestMethod.POST)
    Map<String,Object> getAgentUserByPhone(@RequestParam("phone")  String phone){
            return agentUserMapper.getAgentUserByPhone(phone);
    }
    @RequestMapping(value = "/createAgentToken", method = RequestMethod.POST)
    String createAgentToken(@RequestParam("agent")  String agent, @RequestBody Map<String, Object> user){
        try {
            UserAgentInfo userAgentInfo = UserAgentUtil.getUasParser().parse(
                    agent);
            StringBuilder sb = new StringBuilder();
            sb.append(tokenPrefix);//统一前缀
            String agentStr = "";
            if (userAgentInfo.getDeviceType().equals(UserAgentInfo.UNKNOWN)) {
                if (UserAgentUtil.CheckAgent(agent)) {
                    sb.append("MOBILE-");
                    agentStr = "MOBILE";
                } else {
                    sb.append("PC-");
                    agentStr = "PC";
                }
            } else if (userAgentInfo.getDeviceType()
                    .equals("Personal computer")) {
                sb.append("PC-");
                agentStr = "PC";
            } else{
                agentStr = "MOBILE";
                sb.append("MOBILE-");
            }
//			sb.append(user.getUserCode() + "-");
            sb.append(MD5.getMd5(user.get("phone").toString(),32) + "-");//加密用户名称
            sb.append(user.get("id") + "-");
            sb.append(MD5.getMd5(user.get("id").toString(), 6)
                    + "-");
            sb.append(MD5.getMd5(agentStr, 6));// 识别客户端的简化实现——6位MD5码

            return sb.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    @RequestMapping(value = "/saveAgentToken", method = RequestMethod.POST)
    void saveAgentToken(@RequestParam("token") String token,@RequestBody Map<String, Object> user){
        if (token.startsWith(tokenPrefix+"PC-"))
            redisUtils.set(token, 2*60*60, JSON.toJSONString(user));
        else
            redisUtils.set(token, JSON.toJSONString(user));// 手机认证信息永不失效
    }

    @RequestMapping(value = "/getAgentUserByInviCode", method = RequestMethod.POST)
    Map<String,Object> getAgentUserByInviCode(@RequestParam("parent_invitation_code") String parent_invitation_code){
        return  agentUserMapper.getAgentUserByInviCode(parent_invitation_code);
    }
    @RequestMapping(value = "/getAearningByBCode", method = RequestMethod.POST)
    List<Map<String,Object>> getAearningByBCode(@RequestBody Map<String, Object> param){
        return agentUserMapper.getAearningByBCode(param);
    }

    @RequestMapping(value = "/getAgentAreasByAgentUserId", method = RequestMethod.POST)
    List<Map<String,Object>> getAgentAreasByAgentUserId(@RequestParam("id")String id){
        return agentUserMapper.getAgentAreasByAgentUserId(id);
    }

    @RequestMapping(value = "/checkIdCardNum", method = RequestMethod.POST)
    int checkIdCardNum(@RequestParam("idCard")String idCard){
        return agentUserMapper.checkIdCardNum(idCard);
    }
    @RequestMapping(value = "/updateAgent", method = RequestMethod.POST)
    int updateAgent(@RequestBody Map<String, Object> user){
        return agentUserMapper.updateAgent(user);
    }


    @RequestMapping(value = "/getAgentLevelPays", method = RequestMethod.POST)
    List<Map<String,Object>> getAgentLevelPays(@RequestParam("id")String id){
        return  agentUserMapper.getAgentLevelPays(id);
    }
    @RequestMapping(value = "/getSetOrderList", method = RequestMethod.POST)
    List<Map<String,Object>> getSetOrderList(@RequestParam("id")String id){
        return  agentUserMapper.getSetOrderList(id);
    }

    @RequestMapping(value = "/getAgentUsersByParentInviCode", method = RequestMethod.POST)
    List<Map<String,Object>> getAgentUsersByParentInviCode(@RequestParam("parent_invitation_code") String parent_invitation_code){
        return agentUserMapper.getAgentUsersByParentInviCode(parent_invitation_code);
    }



    @RequestMapping(value = "/getAgentPayByOutNum", method = RequestMethod.POST)
    int getAgentPayByOutNum(@RequestParam("outNum") String outNum){
        return  agentUserMapper.getAgentPayByOutNum(outNum);
    }
    @RequestMapping(value = "/insertAgentPay", method = RequestMethod.POST)
    int insertAgentPay(@RequestBody Map<String, Object> param){
        return  agentUserMapper.insertAgentPay(param);
    }


}
